//
//  AutoRemoveMessageView.m
//  Training-Student
//
//  Created by dev on 12-12-1.
//
//

#import "AutoRemoveMessageView.h"
#import <QuartzCore/QuartzCore.h>
#import "UICreator.h"

@interface AutoRemoveMessageView()
@property (nonatomic, copy) void(^actionAfterCompletion)();
@end

@implementation AutoRemoveMessageView

+ (void)show:(NSString*)message withContainerView:(UIView*)containerView completion:(void (^)(void))action
{
    [self show:message withContainerView:containerView font:[UIFont systemFontOfSize:30] fontColor:[UIColor whiteColor] completion:action];
}

+ (void)show:(NSString*)message withContainerView:(UIView*)containerView duration:(NSTimeInterval) duration completion:(void (^)(void))action
{
    [[[AutoRemoveMessageView alloc] init] show:message withContainerView:containerView font:[UIFont systemFontOfSize:30] fontColor:[UIColor whiteColor] duration:duration completion:action];
}

+ (void)show:(NSString*)message
withContainerView:(UIView*)containerView
        font:(UIFont*)font
   fontColor:(UIColor*)fontColor
    completion:(void (^)(void))action
{
    return [[[AutoRemoveMessageView alloc] init] show:message withContainerView:containerView font:font fontColor:fontColor completion:action];
}

- (void)show:(NSString*)message withContainerView:(UIView*)containerView font:(UIFont*)font fontColor:(UIColor*)fontColor completion:(void (^)(void))action
{
    [self show:message withContainerView:containerView font:font fontColor:fontColor duration:.5 completion:action];
}

- (void)show:(NSString*)message withContainerView:(UIView*)containerView font:(UIFont*)font fontColor:(UIColor*)fontColor duration:(NSTimeInterval) duration completion:(void (^)(void))action
{
    if (!containerView) {
        containerView = [UIApplication sharedApplication].keyWindow;
    }
    
    self.actionAfterCompletion = action;
    
    float widthLimit = containerView.frame.size.width - 80;
    CGSize size = [message sizeWithFont:font constrainedToSize:CGSizeMake(widthLimit, MAXFLOAT)];
    UIView *bkView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width + 40, size.height + 20)];
    [containerView addSubview:bkView];
    bkView.backgroundColor = [UIColor blackColor];
    bkView.alpha = .7;
    bkView.layer.cornerRadius = 10;
    bkView.center = CGPointMake(containerView.frame.size.width/2, containerView.frame.size.height/2);
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    [containerView addSubview:label];
    label.center = bkView.center;
    label.backgroundColor = [UIColor clearColor];
    label.text = message;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = fontColor;
    label.font = font;
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    
    __block float scale = 1.2;
    bkView.transform = CGAffineTransformScale(bkView.transform, scale, scale);
    label.transform = CGAffineTransformScale(label.transform, scale, scale);
    
    NSArray *viewArray = @[bkView, label];
    [self performSelector:@selector(animateMessageWithViews:) withObject:viewArray afterDelay:duration];
    
//    [UIView animateWithDuration:.5 animations:^{
//        scale = 1/scale;
//        bkView.transform = CGAffineTransformScale(bkView.transform, scale, scale);
//        label.transform = CGAffineTransformScale(label.transform, scale, scale);
//    } completion:^(BOOL finished) {
//        NSArray *viewArray = @[bkView, label];
//        [self performSelector:@selector(animateMessageWithViews:) withObject:viewArray afterDelay:duration];
//    }];
}

- (void)animateMessageWithViews:(NSArray*)views
{
    [UIView animateWithDuration:.2 animations:^{
        for (int i=0; i<views.count; i++) {
            ((UIView*)views[i]).alpha = 0;
        }
    } completion:^(BOOL finished) {
        for (int i=0; i<views.count; i++) {
            [((UIView*)views[i]) removeFromSuperview];
        }
        
        if (self.actionAfterCompletion) {
            self.actionAfterCompletion();
        }
    }];
}

@end
